<template>
  <div class="app-container customer-div">
    <el-tabs v-model="tab">
      <el-tab-pane label="用户购买频率" name="a1">
        <div style="margin-top: 20px">
          <!--搜索-->
          <el-form :inline="true" :model="params" size="mini" label-width="100px">
            <el-form-item label="客户编号：">
              <el-input v-model="params.customerNumber"></el-input>
<!--              <el-select-->
<!--                      clearable-->
<!--                      v-model="params.customerId"-->
<!--                      filterable-->
<!--                      remote-->
<!--                      reserve-keyword-->
<!--                      placeholder="请输入关键词"-->
<!--                      :remote-method="remoteMethod1"-->
<!--                      :loading="loading">-->
<!--                <el-option-->
<!--                        v-for="item in customerList"-->
<!--                        :key="item.customerId"-->
<!--                        :label="item.customerName"-->
<!--                        :value="item.customerId">-->
<!--                </el-option>-->
<!--              </el-select>-->
            </el-form-item>
            <el-form-item label="年份：">
              <el-date-picker
                      :clearable="false"
                      style="width:150px"
                      v-model="params.buyYear[0]"
                      value-format="yyyy"
                      type="year"
                      :picker-options="startDatePicker"
                      placeholder="开始年份">
              </el-date-picker>
              <span style="padding:0 10px">-</span>
              <el-date-picker
                      value-format="yyyy"
                      :clearable="false"
                      style="width:150px"
                      v-model="params.buyYear[1]"
                      type="year"
                      :picker-options="endDatePicker"
                      placeholder="结束年份">
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button
                      @click="handleSearchList()"
                      type="primary">
                查询
              </el-button>
            </el-form-item>
          </el-form>
        </div>
        <!--数据列表-->
        <div class="table-container">
          <el-table
                  v-loading="listLoading"
                  :data="list"
                  style="margin-top: 20px">
            <el-table-column  label="客户编号" align="center" min-width="300">
              <template slot-scope="scope">
                <div>{{scope.row.customerNumber }}</div>
                <!--是否有权限-->
                <div v-if="scope.row.isShowCustomerName">{{scope.row.customerName}}</div>
              </template>
            </el-table-column>
            <el-table-column  label="总次数" align="center" min-width="100">
              <template slot-scope="scope">
                {{scope.row.buyAllNum}}
              </template>
            </el-table-column>
            <el-table-column  label="最低购频次" align="center" min-width="150">
              <template slot-scope="scope">
                {{scope.row.minIntervalDays}}天/次
              </template>
            </el-table-column>
            <el-table-column  label="最高购频次" align="center" min-width="150">
              <template slot-scope="scope">
                {{scope.row.maxIntervalDays}}天/次
              </template>
            </el-table-column>
            <el-table-column  label="平均频次" align="center" min-width="150">
              <template slot-scope="scope">
                {{scope.row.perIntervalDays}}天/次
              </template>
            </el-table-column>
            <el-table-column  label="统计时间" align="center" min-width="100">
              <template slot-scope="scope">
                {{scope.row.buyYear}}年
              </template>
            </el-table-column>
            <el-table-column
                    fixed="right"
                    label="操作"
                    align="center"
                    width="150">
              <template slot-scope="scope">
                <el-button
                        @click="toInfo(scope.row,'1')"
                        type="text"
                        size="mini">查看
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pagination-container">
          <el-pagination
                  background
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                  layout="total, sizes,prev, pager, next,jumper"
                  :current-page.sync="params.pageNum"
                  :page-size="params.pageSize"
                  :page-sizes="[10,15,20]"
                  :total="count">
          </el-pagination>
        </div>
      </el-tab-pane>
      <el-tab-pane label="商品购买频率" name="a2">
        <div style="margin-top: 20px">
          <!--搜索-->
          <el-form :inline="true" :model="params2" size="mini" label-width="100px">
            <el-form-item label="客户编号：">
              <el-input v-model="params2.customerNumber"></el-input>
<!--              <el-select-->
<!--                      clearable-->
<!--                      v-model="params2.customerId"-->
<!--                      filterable-->
<!--                      remote-->
<!--                      reserve-keyword-->
<!--                      placeholder="请输入关键词"-->
<!--                      :remote-method="remoteMethod1"-->
<!--                      :loading="loading2">-->
<!--                <el-option-->
<!--                        v-for="item in customerList2"-->
<!--                        :key="item.customerId"-->
<!--                        :label="item.customerName"-->
<!--                        :value="item.customerId">-->
<!--                </el-option>-->
<!--              </el-select>-->
            </el-form-item>
            <el-form-item label="牌号：">
              <el-select v-model="params2.kingGoodsId" filterable placeholder="请选择" clearable>
                <el-option
                        v-for="item in goodsList"
                        :key="item.kingId"
                        :label="item.goodsSn"
                        :value="item.kingId">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="年份：">
              <el-date-picker
                      :clearable="false"
                      style="width:150px"
                      v-model="params2.buyYear[0]"
                      value-format="yyyy"
                      type="year"
                      :picker-options="startDatePicker"
                      placeholder="开始年份">
              </el-date-picker>
              <span style="padding:0 10px">-</span>
              <el-date-picker
                      value-format="yyyy"
                      :clearable="false"
                      style="width:150px"
                      v-model="params2.buyYear[1]"
                      type="year"
                      :picker-options="endDatePicker"
                      placeholder="结束年份">
              </el-date-picker>
            </el-form-item>

            <el-form-item>
              <el-button
                      @click="handleSearchList2()"
                      type="primary">
                查询
              </el-button>
            </el-form-item>
          </el-form>
        </div>
        <!--数据列表-->
        <div class="table-container">
          <el-table
                  v-loading="listLoading"
                  :data="list2"
                  style="margin-top: 20px">
            <el-table-column  label="客户编号" align="center" min-width="300">
              <template slot-scope="scope">
                <div>{{scope.row.customerNumber }}</div>
                <!--是否有权限-->
                <div v-if="scope.row.isShowCustomerName">{{scope.row.customerName}}</div>
              </template>
            </el-table-column>
            <el-table-column  label="牌号" align="center" min-width="150">
              <template slot-scope="scope">
                {{scope.row.goodsSn}}
              </template>
            </el-table-column>
            <el-table-column  label="总次数" align="center" min-width="100">
              <template slot-scope="scope">
                {{scope.row.buyAllNum}}
              </template>
            </el-table-column>
            <el-table-column  label="最低购频次" align="center" min-width="150">
              <template slot-scope="scope">
                {{scope.row.minIntervalDays}}天/次
              </template>
            </el-table-column>
            <el-table-column  label="最高购频次" align="center" min-width="150">
              <template slot-scope="scope">
                {{scope.row.maxIntervalDays}}天/次
              </template>
            </el-table-column>
            <el-table-column  label="平均频次" align="center" min-width="150">
              <template slot-scope="scope">
                {{scope.row.perIntervalDays}}天/次
              </template>
            </el-table-column>
            <el-table-column  label="统计时间" align="center" min-width="100">
              <template slot-scope="scope">
                {{scope.row.buyYear}}年
              </template>
            </el-table-column>
            <el-table-column
                    fixed="right"
                    label="操作"
                    align="center"
                    width="150">
              <template slot-scope="scope">
                <el-button
                        @click="toInfo(scope.row,'2')"
                        type="text"
                        size="mini">查看
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pagination-container">
          <el-pagination
                  background
                  @size-change="handleSizeChange2"
                  @current-change="handleCurrentChange2"
                  layout="total, sizes,prev, pager, next,jumper"
                  :current-page.sync="params2.pageNum"
                  :page-size="params2.pageSize"
                  :page-sizes="[10,15,20]"
                  :total="count2">
          </el-pagination>
        </div>
      </el-tab-pane>
      <el-tab-pane label="品类购买频率" name="a3">
        <div style="margin-top: 20px">
          <!--搜索-->
          <el-form :inline="true" :model="params3" size="mini" label-width="100px">
            <el-form-item label="客户编号：">
              <el-input v-model="params3.customerNumber"></el-input>
<!--              <el-select-->
<!--                      clearable-->
<!--                      v-model="params3.customerId"-->
<!--                      filterable-->
<!--                      remote-->
<!--                      reserve-keyword-->
<!--                      placeholder="请输入关键词"-->
<!--                      :remote-method="remoteMethod1"-->
<!--                      :loading="loading3">-->
<!--                <el-option-->
<!--                        v-for="item in customerList3"-->
<!--                        :key="item.customerId"-->
<!--                        :label="item.customerName"-->
<!--                        :value="item.customerId">-->
<!--                </el-option>-->
<!--              </el-select>-->
            </el-form-item>
            <el-form-item label="品类：">
              <el-input v-model="params3.goodsName"></el-input>
            </el-form-item>
            <el-form-item label="年份：">
              <el-date-picker
                      :clearable="false"
                      style="width:150px"
                      v-model="params3.buyYear[0]"
                      value-format="yyyy"
                      type="year"
                      :picker-options="startDatePicker"
                      placeholder="开始年份">
              </el-date-picker>
              <span style="padding:0 10px">-</span>
              <el-date-picker
                      value-format="yyyy"
                      :clearable="false"
                      style="width:150px"
                      v-model="params3.buyYear[1]"
                      type="year"
                      :picker-options="endDatePicker"
                      placeholder="结束年份">
              </el-date-picker>
            </el-form-item>

            <el-form-item>
              <el-button
                      @click="handleSearchList3()"
                      type="primary">
                查询
              </el-button>
            </el-form-item>
          </el-form>
        </div>
        <!--数据列表-->
        <div class="table-container">
          <el-table
                  v-loading="listLoading3"
                  :data="list3"
                  style="margin-top: 20px">
            <el-table-column  label="客户编号" align="center" min-width="300">
              <template slot-scope="scope">
                <div>{{scope.row.customerNumber }}</div>
                <!--是否有权限-->
                <div v-if="scope.row.isShowCustomerName">{{scope.row.customerName}}</div>
              </template>
            </el-table-column>
            <el-table-column  label="品类" align="center" min-width="150">
              <template slot-scope="scope">
                {{scope.row.goodsName}}
              </template>
            </el-table-column>
            <el-table-column  label="总次数" align="center" min-width="100">
              <template slot-scope="scope">
                {{scope.row.buyAllNum}}
              </template>
            </el-table-column>
            <el-table-column  label="最低购频次" align="center" min-width="150">
              <template slot-scope="scope">
                {{scope.row.minIntervalDays}}天/次
              </template>
            </el-table-column>
            <el-table-column  label="最高购频次" align="center" min-width="150">
              <template slot-scope="scope">
                {{scope.row.maxIntervalDays}}天/次
              </template>
            </el-table-column>
            <el-table-column  label="平均频次" align="center" min-width="150">
              <template slot-scope="scope">
                {{scope.row.perIntervalDays}}天/次
              </template>
            </el-table-column>
            <el-table-column  label="统计时间" align="center" min-width="100">
              <template slot-scope="scope">
                {{scope.row.buyYear}}年
              </template>
            </el-table-column>
            <el-table-column
                    fixed="right"
                    label="操作"
                    align="center"
                    width="150">
              <template slot-scope="scope">
                <el-button
                        @click="toInfo(scope.row,'3')"
                        type="text"
                        size="mini">查看
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pagination-container">
          <el-pagination
                  background
                  @size-change="handleSizeChange3"
                  @current-change="handleCurrentChange3"
                  layout="total, sizes,prev, pager, next,jumper"
                  :current-page.sync="params3.pageNum"
                  :page-size="params3.pageSize"
                  :page-sizes="[10,15,20]"
                  :total="count3">
          </el-pagination>
        </div>
      </el-tab-pane>
      <el-tab-pane label="购买提醒" name="a4">
        <div style="margin-top: 20px">
          <!--搜索-->
          <el-form :inline="true" :model="params4" size="mini" label-width="100px">
            <el-form-item label="客户编号：">
              <el-input v-model="params4.customerNumber"></el-input>
<!--              <el-select-->
<!--                      clearable-->
<!--                      v-model="params4.customerId"-->
<!--                      filterable-->
<!--                      remote-->
<!--                      reserve-keyword-->
<!--                      placeholder="请输入关键词"-->
<!--                      :remote-method="remoteMethod1"-->
<!--                      :loading="loading4">-->
<!--                <el-option-->
<!--                        v-for="item in customerList4"-->
<!--                        :key="item.customerId"-->
<!--                        :label="item.customerName"-->
<!--                        :value="item.customerId">-->
<!--                </el-option>-->
<!--              </el-select>-->
            </el-form-item>

            <el-form-item label="提醒时间段：" label-width="130px">
              <el-date-picker
                      :default-time="['00:00:00', '23:59:59']"
                      v-model="params4.nextBuyDate"
                      type="daterange"
                      format="yyyy 年 MM 月 dd 日"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      range-separator="至"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button
                      @click="handleSearchList4()"
                      type="primary">
                查询
              </el-button>
            </el-form-item>
          </el-form>
        </div>
        <!--数据列表-->
        <div class="table-container">
          <el-table
                  v-loading="listLoading4"
                  :data="list4"
                  style="margin-top: 20px">
            <el-table-column  label="客户编号" align="center" min-width="300">
              <template slot-scope="scope">
                <div>{{scope.row.customerNumber }}</div>
                <!--是否有权限-->
                <div v-if="scope.row.isShowCustomerName">{{scope.row.customerName}}</div>
              </template>
            </el-table-column>
            <el-table-column  label="商品" align="center" min-width="150">
              <template slot-scope="scope">
                {{scope.row.goodsName}}
              </template>
            </el-table-column>
            <el-table-column  label="上次购买时间" align="center" min-width="100">
              <template slot-scope="scope">
                {{scope.row.buyDate}}
              </template>
            </el-table-column>
            <el-table-column  label="预计下次购买时间" align="center" min-width="150">
              <template slot-scope="scope">
                {{scope.row.nextBuyDate}}
              </template>
            </el-table-column>
            <el-table-column  label="提醒状态" align="center" min-width="150">
              <template slot-scope="scope">
                {{scope.row.remindState == 1 ? '已提醒' : '未提醒'}}
              </template>
            </el-table-column>
            <el-table-column  label="发送时间" align="center" min-width="150">
              <template slot-scope="scope">
                {{scope.row.createDate}}
              </template>
            </el-table-column>
            <el-table-column  label="业务员" align="center" min-width="100">
              <template slot-scope="scope">
                {{scope.row.userName}}
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pagination-container">
          <el-pagination
                  background
                  @size-change="handleSizeChange4"
                  @current-change="handleCurrentChange4"
                  layout="total, sizes,prev, pager, next,jumper"
                  :current-page.sync="params4.pageNum"
                  :page-size="params4.pageSize"
                  :page-sizes="[10,15,20]"
                  :total="count4">
          </el-pagination>
        </div>
      </el-tab-pane>

    </el-tabs>

  </div>
</template>

<script>
  import { getOrderUsersList,buyRateByYear,getCustomerOption,buyClassRateByYear,buyGoodsRateByYear,buyRemindList,orderGoodsList} from '@/api/partner'
  import {formatDate} from '@/utils/date';
  import store from '@/store'
  import { mapGetters } from 'vuex'

  export default {
    name: "buyFrequencyReport",
    data(){
      return{
        roles:store.getters.roles ? store.getters.roles : [],
        params:{
          pageNum:1,
          pageSize:10,
          customerId:'',
          buyYear:[],
          customerNumber:""
        },
        list:[],
        count:0,
        listLoading:false,
        tab:'a1',
        startDatePicker: this.beginDate(),
        endDatePicker: this.processDate(),
        userOptionList:[],
        title:[],

        list2:[],
        count2:0,
        listLoading2:false,
        params2:{
          pageNum:1,
          pageSize:10,
          customerId:'',
          goodsSn:'',
          buyYear:[],
          kingGoodsId:'',
          customerNumber:""
        },

        list3:[],
        count3:0,
        listLoading3:false,
        params3:{
          pageNum:1,
          pageSize:10,
          customerId:'',
          goodsName:'',
          buyYear:[],
          customerNumber:""
        },

        list4:[],
        count4:0,
        listLoading4:false,
        params4:{
          pageNum:1,
          pageSize:10,
          customerId:'',
          nextBuyDate:[],
          tab:'all',
          customerNumber:""
        },
        customerList:[],
        customerList2:[],
        customerList3:[],
        customerList4:[],
        loading:false,
        loading2:false,
        loading3:false,
        loading4:false,
        goodsList:[],
      }
    },
    computed: {
      ...mapGetters([
        'name',
        'mobile',
        'userId'
      ]),


    },
    created() {
      this.getListData();
      this.getListData2();
      this.getListData3();
      this.getListData4();
      orderGoodsList().then((res)=>{
        this.goodsList = res.list
      })
    },
    filters: {
      formatTime(time) {
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd')
      },
    },
    methods:{
      //获取客户列表
      remoteMethod1(query) {
        if(query && query!= ''){
          if(this.tab == 'a1'){
            this.loading = true;
          }else if(this.tab == 'a2'){
            this.loading2 = true;
          }else if(this.tab == 'a3'){
            this.loading3 = true;
          }else if(this.tab == 'a4'){
            this.loading4 = true;
          }
          getCustomerOption({keywords:query}).then(response => {
            if(this.tab == 'a1'){
              this.customerList = response;
              this.loading = false;
            }else if(this.tab == 'a2'){
              this.customerList2 = response;
              this.loading2 = false;
            }else if(this.tab == 'a3'){
              this.customerList3 = response;
              this.loading3 = false;
            }else if(this.tab == 'a4'){
              this.customerList4 = response;
              this.loading4 = false;
            }

          }).catch(()=>{
            this.loading = false;
            this.loading2 = false;
            this.loading3 = false;
            this.loading4 = false;
          });
        }
      },
      handleSearchList2(){
        this.params2.pageNum = 1;
        this.getListData2();
      },

      handleSizeChange2(val){
        this.params2.pageNum = 1;
        this.params2.pageSize = val;
        this.getListData2();
      },
      handleCurrentChange2(val){
        this.params2.pageNum = val;
        this.getListData2();
      },

      getListData2(){
        this.listLoading2 = true;
        buyGoodsRateByYear(this.params2).then((res)=>{
          this.list2 = res.list;
          this.count2 = res.count;
          this.listLoading2 = false;
        }).catch(()=>{
          this.listLoading2 = false;
        })
      },

      handleSearchList3(){
        this.params3.pageNum = 1;
        this.getListData3();
      },

      handleSizeChange3(val){
        this.params3.pageNum = 1;
        this.params3.pageSize = val;
        this.getListData3();
      },
      handleCurrentChange3(val){
        this.params3.pageNum = val;
        this.getListData3();
      },

      getListData3(){
        this.listLoading3 = true;
        buyClassRateByYear(this.params3).then((res)=>{
          this.list3 = res.list;
          this.count3 = res.count;
          this.listLoading3 = false;
        }).catch(()=>{
          this.listLoading3 = false;
        })
      },

      handleSearchList4(){
        this.params4.pageNum = 1;
        this.getListData4();
      },

      handleSizeChange4(val){
        this.params4.pageNum = 1;
        this.params4.pageSize = val;
        this.getListData4();
      },
      handleCurrentChange4(val){
        this.params4.pageNum = val;
        this.getListData4();
      },

      getListData4(){
        this.listLoading4 = true;
        buyRemindList(this.params4).then((res)=>{
          this.list4 = res.list;
          this.count4 = res.count;
          this.listLoading4 = false;
        }).catch(()=>{
          this.listLoading4 = false;
        })
      },



      beginDate(){
        const self = this;
        return {
          disabledDate(time){
            let key;
            if(self.tab == 'a1'){
              key = 'params'
            }else if(self.tab == 'a2'){
              key = 'params2'
            }else if(self.tab == 'a3'){
              key = 'params3'
            }
            if (self[key].buyYear[1]) {  //如果结束时间不为空，则小于结束时间
              let starttime = new Date(time).getFullYear() + '-' + '01';
              return new Date(self[key].buyYear[1]).getTime() < new Date(starttime).getTime()
            } else {
              // return time.getTime() > Date.now()//开始时间不选时，结束时间最大值小于等于当天
            }
          }
        }
      },
      processDate() {
        const  self = this;
        return {
          disabledDate(time) {
            let key;
            if(self.tab == 'a1'){
              key = 'params'
            }else if(self.tab == 'a2'){
              key = 'params2'
            }else if(self.tab == 'a3'){
              key = 'params3'
            }
            if (self[key].buyYear[0]) {  //如果开始时间不为空，则结束时间大于开始时间
              let starttime = new Date(self[key].buyYear[0]).getFullYear() + '-' + '01';
              return new Date(starttime).getTime() > time.getTime()
            } else {
              // return time.getTime() > Date.now()//开始时间不选时，结束时间最大值小于等于当天
            }
          }
        }
      },

      handleSearchList(){
        this.params.pageNum = 1;
        this.getListData();
      },

      handleSizeChange(val){
        this.params.pageNum = 1;
        this.params.pageSize = val;
        this.getListData();
      },
      handleCurrentChange(val){
        this.params.pageNum = val;
        this.getListData();
      },

      getListData(){
        this.listLoading = true;
        buyRateByYear(this.params).then((res)=>{
          this.list = res.list;
          this.count = res.count;
          this.title = res.title;
          this.listLoading = false;
        }).catch(()=>{
          this.listLoading = false;
        })
      },

      toInfo(item,type){
        let routeData = '';
        switch (type) {
          case '1':
            routeData = this.$router.resolve({name:'buyFrequencyInfo',query:{customerId:item.customerId,customerName:item.customerName,buyDate:item.buyYear}});
            break;
          case '2':
            routeData = this.$router.resolve({name:'buyFrequencyInfo',query:{customerId:item.customerId,customerName:item.customerName,goodsSn:item.goodsSn,buyDate:item.buyYear}});
            break;
          case '3':
            routeData = this.$router.resolve({name:'buyFrequencyInfo',query:{customerId:item.customerId,customerName:item.customerName,goodsName:item.goodsName,buyDate:item.buyYear}});
            break;
        }

        window.open(routeData.href);
      },

    },
  }
</script>

<style scoped lang="scss">
  .avatar-uploader{
    margin-bottom:10px;
    display: inline-block;
    width:80px;
    height:80px;
    border:1px solid #DCDFE6;
    text-align: center;
    line-height: 80px;
  }
</style>
